package com.jsz.peini.service.task;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.jsz.peini.bean.order.OrderInfo;
import com.jsz.peini.bean.task.PartyInfo;
import com.jsz.peini.bean.task.PartyInfoExample;
import com.jsz.peini.bean.task.PartyUser;
import com.jsz.peini.bean.task.PartyUserExample;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.common.meta.OrderType;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.mapper.order.OrderInfoMapper;
import com.jsz.peini.mapper.task.PartyInfoMapper;
import com.jsz.peini.mapper.task.PartyUserMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.mapper.user.UserSmsMapper;
import com.jsz.peini.service.user.UserInfoService;

@Service
public class PartyService {
	@Resource
	private PartyInfoMapper partyInfoMapper;
	@Resource
	private PartyUserMapper partyUserMapper;
	@Resource
	private UserInfoService userInfoService;
	@Resource
	private UserInfoMapper userInfoMapper;
	@Resource
	private UserSmsMapper userSmsMapper;
	@Resource
	private OrderInfoMapper orderInfoMapper;
	
	@Transactional
	public void createParty(PartyInfo partyInfo){
		partyInfoMapper.insertSelective(partyInfo);
		PartyUser partyUser = new PartyUser();
		partyUser.setIsOrganiser(true);
		partyUser.setPartyId(partyInfo.getId());
		partyUser.setUserId(partyInfo.getUserId());
		partyUserMapper.insertSelective(partyUser);
	}
	
	public PartyInfo getPartyInfo(Integer id){
		PartyInfo party = partyInfoMapper.selectByPrimaryKey(id);
		if(party == null)
			return null;
		UserInfo user = userInfoMapper.selectByPrimaryKey(party.getUserId());
		party.setUserInfo(user);
		PartyUserExample partyUserExample = new PartyUserExample();
		partyUserExample.createCriteria().andPartyIdEqualTo(party.getId());
		List<Map<String,String>> memberList = partyUserMapper.selectPartyUsers(party.getId());
		party.setMembers(memberList);
		party.setMemberNum(memberList.size());
		return party;
	}
	
	public List<PartyInfo> getPartyInfoList(boolean offical,int pageSize, int pageNum){
		PageHelper.startPage(pageNum, pageSize);
		PartyInfoExample example = new PartyInfoExample();
		example.createCriteria().andIsdeleteEqualTo(false).andOfficialEqualTo(offical);
		example.setOrderByClause("appointed_time asc");
		List<PartyInfo> partyList = partyInfoMapper.selectByExample(example);
		PartyUserExample partyUserExample = new PartyUserExample();
		for(PartyInfo party : partyList){
			UserInfo user = userInfoMapper.selectByPrimaryKey(party.getUserId());
			party.setUserInfo(user);
			partyUserExample.createCriteria().andPartyIdEqualTo(party.getId());
			List<Map<String,String>> memberList = partyUserMapper.selectPartyUsers(party.getId());
			party.setMembers(memberList);
			party.setMemberNum(memberList.size());
		}
		return partyList;
	}
	
	@Transactional
	public OrderInfo createPartyUser(PartyInfo partyInfo, UserInfo userInfo){
		PartyUser partyUser = new PartyUser();
		partyUser.setPartyId(partyInfo.getId());
		partyUser.setUserId(userInfo.getId());
		if(partyInfo.getUserId().equals(userInfo.getId())){
			partyUser.setIsOrganiser(true);
		}else{
			partyUser.setIsOrganiser(false);
		}
		if(partyInfo.getPrePay() > 0){
			partyUser.setStatus(0);
		}else{
			partyUser.setStatus(1);
		}
		partyUserMapper.insertSelective(partyUser);
		
		if(partyInfo.getPrePay() > 0){
			Date now = new Date();
			OrderInfo order = new OrderInfo();
			order.setAllMoney(partyInfo.getPrePay());
			order.setPayMoney(partyInfo.getPrePay());
			order.setUserId(userInfo.getId());
			order.setOrderStatus(0);
			order.setUserPhone(userInfo.getUserPhone());
			order.setOrderTime(now);
			order.setOrderType(OrderType.PARTY_PREPAY);
			order.setCreateTime(now);
			orderInfoMapper.insertSelective(order);
			order.setOrderCode(PeiniUtils.generateOrderCode(order));
			orderInfoMapper.updateByPrimaryKeySelective(order);
			return order;
		}
		return null;
	}
	
	public Integer selectCntByBetweenTime(String userId, Date appointedTime){
		return partyInfoMapper.selectCntByBetweenTime(userId, appointedTime);
	}
	
	public void setUserPrePayed(OrderInfo orderInfo){
		partyInfoMapper.setUserPrePayed(orderInfo.getId().intValue(), orderInfo.getUserId());
	}
	
	public void setPartyPayed(OrderInfo orderInfo){
		PartyInfoExample example = new PartyInfoExample();
		example.createCriteria().andOrderIdEqualTo(orderInfo.getId().intValue());
		PartyInfo party = new PartyInfo();
		party.setPartyStatus(3);
		partyInfoMapper.updateByExampleSelective(party, example);
	}
	
	public PartyUser getPartyUser(int partyId, String userId){
		PartyUserExample example = new PartyUserExample();
		example.createCriteria().andUserIdEqualTo(userId).andPartyIdEqualTo(partyId);
		List<PartyUser> list = partyUserMapper.selectByExample(example);
		if(list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	@Transactional
	public void cancelParty(PartyUser partyUser){
		if(partyUser.getIsOrganiser()){  //组织者取消
			PartyInfo partyInfo = this.getPartyInfo(partyUser.getPartyId());
			partyInfo.setPartyStatus(5);
			partyInfoMapper.updateByPrimaryKeySelective(partyInfo);
			PartyUserExample example = new PartyUserExample();
			example.createCriteria().andPartyIdEqualTo(partyInfo.getId()).andStatusEqualTo(1);
			List<PartyUser> list = partyUserMapper.selectByExample(example);
			for(PartyUser pu : list){
				if(pu.getIsOrganiser()){
					pu.setCancelType(1);
				}else{
					pu.setCancelType(2);
				}
				if(pu.getPrePayed()){
					pu.setStatus(2);
				}
				partyUserMapper.updateByPrimaryKeySelective(pu);
			}
		}else{
			partyUserMapper.updateByPrimaryKeySelective(partyUser);
		}
	}
	
	@Transactional
	public OrderInfo payParty(PartyInfo partyInfo, UserInfo userInfo){
		Date now = new Date();
		OrderInfo order = new OrderInfo();
		order.setAllMoney(partyInfo.getPrePay());
		order.setPayMoney(partyInfo.getPrePay());
		order.setUserId(partyInfo.getUserId());
		order.setOrderStatus(0);
		order.setUserPhone(userInfo.getUserPhone());
		order.setOrderTime(now);
		if(partyInfo.getOfficial()){
			order.setOrderType(OrderType.OFFICAL_PARTY);
		}else{
			order.setOrderType(OrderType.PARTY_ORDER);
		}
		order.setCreateTime(now);
		orderInfoMapper.insertSelective(order);
		order.setOrderCode(PeiniUtils.generateOrderCode(order));
		orderInfoMapper.updateByPrimaryKeySelective(order);
		
		return order;
	}

}
